package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Level;
import com.neuedu.service.LevelService;
import com.neuedu.vo.LevelCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api("挂号级别管理")
@RestController
@RequestMapping("/level")
@CrossOrigin
public class LevelController {

    @Resource
    private LevelService levelService;

    @ApiOperation("挂号级别列表")
    @GetMapping("/list")
    public ServerResponse list(LevelCondition levelCondition) {

        System.out.println(levelCondition);
        //调用业务方法
        Page<Level> levelPage = levelService.getLevelList(levelCondition);
        //返回分页对象
        return ServerResponse.success("").data("levelPage", levelPage);
    }

    @ApiOperation("添加挂号级别")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Level level) {

        if(StringUtils.isEmpty(level.getLevelName()))  {
            return ServerResponse.error("挂号级别名称不能为空");
        }
        if(StringUtils.isEmpty(level.getLevelAmount()))  {
            return ServerResponse.error("挂号费用不能为空");
        }

        if(level.getLevelName().length()>10){
            return ServerResponse.error("挂号级别名称的长度不能超过10个字符");
        }

        if(levelService.save(level)){
            return ServerResponse.success("添加挂号级别成功");
        }else{
            return ServerResponse.error("添加挂号级别失败");
        }
    }

    @ApiOperation("根据编号获取挂号级别")
    @GetMapping("/get/{levelId}")
    public ServerResponse get(@PathVariable int levelId) {

        Level level = levelService.getById(levelId);

        if(level!=null){
            return ServerResponse.success("").data("level", level);
        }else{
            return ServerResponse.error("获取挂号级别信息失败");
        }
    }

    @ApiOperation("修改挂号级别")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Level level) {

        if(StringUtils.isEmpty(level.getLevelName()))  {
            return ServerResponse.error("挂号级别名称不能为空");
        }

        if(level.getLevelName().length()>10){
            return ServerResponse.error("挂号级别名称的长度不能超过10个字符");
        }

        if(levelService.updateById(level)){
            return ServerResponse.success("修改挂号级别成功");
        }else{
            return ServerResponse.error("修改挂号级别失败");
        }
    }

    @ApiOperation("删除挂号级别")
    @DeleteMapping("/remove/{levelId}")
    public ServerResponse remove(@PathVariable int levelId) {

        if(levelService.removeById(levelId)){
            return ServerResponse.success("删除挂号级别成功");
        }else{
            return ServerResponse.error("删除挂号级别失败");
        }
    }

    @ApiOperation("判断指定的挂号级别是否可以删除")
    @GetMapping("/removeEnable/{levelId}")
    public ServerResponse removeEnable(@PathVariable int levelId) {

        if(levelService.removeEnable(levelId)){
            return ServerResponse.success("允许删除挂号级别");
        }else{
            return ServerResponse.error("当前挂号级别下存在医生，不能删除");
        }
    }

    @ApiOperation("批量删除挂号级别")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String levelIds) {

        if(levelIds == ""){
            return ServerResponse.error("未选中要删除的挂号级别列表");
        }
        if(levelService.deleteBatch(levelIds)){
            return ServerResponse.success("批量删除挂号级别成功");
        }else{
            return ServerResponse.error("批量删除挂号级别成功失败");
        }
    }


    @ApiOperation("获取全部挂号级别列表")
    @GetMapping("/listAll")
    public ServerResponse listAll() {

        List<Level> levelList = levelService.list();

        return ServerResponse.success("").data("levelList", levelList);
    }
}